package com.ruoyi.wq.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.wq.mapper.WqActivityMapper;
import com.ruoyi.wq.mapper.WqActivityUserMapper;
import com.ruoyi.wq.domain.WqActivity;
import com.ruoyi.wq.domain.WqActivityUser;
import com.ruoyi.wq.domain.vo.WqActivityUserVO;
import com.ruoyi.wq.service.IWqActivityUserService;

/**
 * 活动报名Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-12
 */
@Service
public class WqActivityUserServiceImpl extends ServiceImpl<WqActivityUserMapper, WqActivityUser> implements IWqActivityUserService {

    @Autowired
    private WqActivityUserMapper wqActivityUserMapper;
    
    @Autowired
    private WqActivityMapper wqActivityMapper;

    /**
     * 查询活动报名
     * 
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public WqActivityUser selectWqActivityUserById(String id)
    {
        return wqActivityUserMapper.selectWqActivityUserById(id);
    }

    /**
     * 查询活动报名列表
     * 
     * @param wqActivityUser 活动报名
     * @return 活动报名
     */
    @Override
    public List<WqActivityUser> selectWqActivityUserList(WqActivityUser wqActivityUser)
    {
        return wqActivityUserMapper.selectWqActivityUserList(wqActivityUser);
    }

    /**
     * 新增活动报名
     * 
     * @param wqActivityUser 活动报名
     * @return 结果
     */
    @Override
    public int insertWqActivityUser(WqActivityUser wqActivityUser)
    {
        wqActivityUser.setCreateTime(DateUtils.getNowDate());
        return wqActivityUserMapper.insertWqActivityUser(wqActivityUser);
    }

    /**
     * 修改活动报名
     * 
     * @param wqActivityUser 活动报名
     * @return 结果
     */
    @Override
    public int updateWqActivityUser(WqActivityUser wqActivityUser)
    {
        wqActivityUser.setUpdateTime(DateUtils.getNowDate());
        return wqActivityUserMapper.updateWqActivityUser(wqActivityUser);
    }

    /**
     * 批量删除活动报名
     * 
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteWqActivityUserByIds(String[] ids)
    {
        return wqActivityUserMapper.deleteWqActivityUserByIds(ids);
    }

    /**
     * 删除活动报名信息
     * 
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteWqActivityUserById(String id)
    {
        return wqActivityUserMapper.deleteWqActivityUserById(id);
    }

	@Override
	public void updateActivityUser(String activityId, Long userId) {
		QueryWrapper<WqActivityUser> qw = new QueryWrapper<>();
		WqActivityUser entity = new WqActivityUser();
		entity.setActivityId(activityId);
		entity.setUserId(userId);
		qw.setEntity(entity);
		WqActivityUser activityUser = baseMapper.selectOne(qw);
		if(activityUser != null){
			activityUser.setStatus("1");
			baseMapper.updateById(activityUser);
		}
	}

	@Override
	public List<SysUser> getUserListByActivityId(String activityId) {
		return baseMapper.getUserListByActivityId(activityId);
	}

	@Override
	public int getManUserCount(String activityId) {
		return baseMapper.getManUserCount(activityId);
	}

	@Override
	public TableDataInfo getPageList(WqActivityUser wqActivityUser) {
		wqActivityUser.setUserId(SecurityUtils.getUserId());
		IPage<WqActivityUserVO> page = baseMapper.selectPageList(Condition.getPage(wqActivityUser), wqActivityUser);
		for(WqActivityUserVO vo : page.getRecords()){
			if(vo.getStartTime() != null && vo.getEndTime() != null){
				vo.setApplyTime(DateUtils.getRangeTime(vo.getStartTime(), vo.getEndTime()));
			}
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Integer getSumByUserId(Long userId) {
		QueryWrapper<WqActivityUser> queryWrapper = new QueryWrapper<>();
		WqActivityUser entity = new WqActivityUser();
		entity.setUserId(userId);
		queryWrapper.setEntity(entity);
		return baseMapper.selectCount(queryWrapper);
	}

	@Override
	public Long getSumByActivityId(String activityId) {
		QueryWrapper<WqActivityUser> queryWrapper = new QueryWrapper<>();
		WqActivityUser entity = new WqActivityUser();
		entity.setActivityId(activityId);
		queryWrapper.setEntity(entity);
		Integer count = baseMapper.selectCount(queryWrapper);
		return Long.valueOf(count);
	}

	@Override
	public TableDataInfo getPageListByActivityId(WqActivityUser wqActivityUser) {
		WqActivity activity = wqActivityMapper.selectById(wqActivityUser.getActivityId());
		if(!activity.getUserId().equals(SecurityUtils.getUserId())){
			throw new ServiceException("只有发起者可以查看");
		}
		IPage<WqActivityUserVO> page = baseMapper.selectPageListByActivityId(Condition.getPage(wqActivityUser), wqActivityUser);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}
}
